package com.systemdevelopment.mainproject.View.Admin;
import com.jfoenix.controls.JFXButton;
import com.jfoenix.controls.JFXComboBox;
import com.jfoenix.controls.JFXListView;
import com.jfoenix.controls.JFXTextField;
import com.systemdevelopment.mainproject.Model.Author;
import com.systemdevelopment.mainproject.Model.Book;
import com.systemdevelopment.mainproject.View.mainUI;
import com.systemdevelopment.mainproject.View.oneInstanceController;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableArray;
import javafx.collections.ObservableList;
import javafx.collections.ObservableSet;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.scene.control.DatePicker;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.stage.FileChooser;
import javafx.stage.Stage;

import java.io.File;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

public class newBookController extends oneInstanceController {


    File coverImg;

    @FXML
    protected JFXListView<Author> selectedAuthors;

    @FXML
    protected JFXComboBox<Author> authorBox;
    @FXML
    protected JFXTextField ISBN;

    @FXML
    private JFXButton addNewBookButton;

    @FXML
    protected JFXButton addAuthorButton;

    @FXML
    protected DatePicker publishDate;

    @FXML
    protected JFXButton newAuthorButton;

    @FXML
    protected JFXTextField publisher;

    @FXML
    protected JFXButton addCoverButton;

    @FXML
    protected JFXTextField bookName;
    @FXML
    protected JFXTextField quantityField;


    @FXML
    protected ImageView coverView;
    protected HashSet<Author> authors=Author.ReadAllAuthorsFromDB();

    @FXML
    protected void  searchAuthor(ActionEvent event) {

    }

    @FXML
    protected void newAuthor(ActionEvent event) {
        mainUI.loadContentPane("./Admin/newAuthorPane.fxml");
    }

    @FXML
    protected void addAuthor(ActionEvent event) {
        selectedAuthors.getItems().add(authorBox.getValue());

    }

    @FXML
    protected void addNewBook(ActionEvent event) {
        Book b=new Book();
        b.setName(bookName.getText());
        HashSet<Author> authorSet = new HashSet<>(selectedAuthors.getItems());
        b.setAuthor(authorSet);
        b.setISBN(ISBN.getText());
        b.setPublicationDate(publishDate.getValue().toString());
        b.setPublisher(publisher.getText());
        b.setAllQuantity(Integer.parseInt(quantityField.getText()));
        b.setNowQuantity(Integer.parseInt(quantityField.getText()));

        try {
            Book.InsertBookIntoDB(b);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @FXML
    protected void addCover(ActionEvent event)
    {
        FileChooser fileChooser = new FileChooser();
        fileChooser.setTitle("Open Resource File");
        coverImg=fileChooser.showOpenDialog(new Stage());
        if(coverImg!=null)
        {
            coverView.setImage(new Image(coverImg.getAbsolutePath()));
            addCoverButton.setVisible(false);
            coverView.setOnMouseClicked(event1 -> addCover(event));
        }

    }

    public void initAuthorBox() {
        authors=Author.ReadAllAuthorsFromDB();
        authorBox.setItems(FXCollections.observableList(new ArrayList<>()));
        selectedAuthors.setItems(FXCollections.observableList(new ArrayList<>()));
        for (Author a:authors) {
            authorBox.getItems().add(a);
        }
    }

    public void deleteSelectedAuthor(ActionEvent actionEvent) {
        selectedAuthors.getItems().removeAll(selectedAuthors.getSelectionModel().getSelectedItems());
    }
}
